Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/app/api/units/[id]/route.ts9 * Module : app10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-11-2612 * Last Modified : 2026-11-2613 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// GET /api/units/{id} — fiche complète d'une unité d'évaluation : identité et43// caractéristiques du rôle, estimation (valeur, fourchette, confiance,44// comparables ajustés), contexte de marché, ventes publiées de l'adresse et45// liens (page, rapports PDF, méthode du coût). C'est l'appel unique de la46// fiche de l'app iOS.47import { NextRequest } from "next/server";48import { salesOfUnit } from "@/lib/db";49import { estimateByUnitId } from "@/lib/estimator";50import { SITE_URL, TYPE_LABELS, TYPE_SLUGS, slugify } from "@/lib/seo";51import { vsRolePct } from "@/lib/ventes";52import { cache, erreur, json } from "@/lib/api";5354export const dynamic = "force-dynamic";5556export async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {57 const { id: brut } = await params;58 const id = decodeURIComponent(brut).trim();59 if (!id) return erreur(400, "id requis");60 try {61 const r = estimateByUnitId(id);62 if (!r || !r.unit) return erreur(404, "unité introuvable", "not_found");63 const u = r.unit;64 const fini = (n: number) => (Number.isFinite(n) ? n : null);65 const sales = salesOfUnit(u.id).map((t) => ({66 id: t.id,67 date: t.date,68 amount: t.amount,69 propertyType: t.property_type,70 valeurRole: t.valeur_role,71 vsRolePct: vsRolePct(t.amount, t.valeur_role),72 }));73 const e = encodeURIComponent(u.id);74 return json(75 {76 unit: {77 ...u,78 typeLabel: TYPE_LABELS[u.typeProp]?.singulier ?? null,79 municipaliteSlug: u.municipalite ? slugify(u.municipalite) : null,80 typeSlug: TYPE_SLUGS[u.typeProp] ?? null,81 },82 // un marché sans modèle ni comparables donne NaN : servi en null83 result: { ...r.result, estimate: fini(r.result.estimate), low: fini(r.result.low), high: fini(r.result.high) },84 context: r.context ?? null,85 sales,86 links: {87 page: `${SITE_URL}/estimation/${e}`,88 pdf: `/api/report?id=${e}`,89 pdfPro: `/api/report/pro?id=${e}`,90 cout: `/cout?property=${e}`,91 },92 },93 cache(300)94 );95 } catch (err) {96 return erreur(500, (err as Error).message);97 }98}99